package com.my.aslife.utils;

import com.my.aslife.module.common.ErrorCode;
import com.my.aslife.module.common.ResponseResult;
import com.my.aslife.module.entity.User;
import com.my.aslife.module.entity.dtos.RegisterDto;
import com.my.aslife.module.entity.dtos.ResetDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;

/**
 * @author mayuan
 * @description TODO
 */
public class MyUtils {

    private MyUtils(){}

    private static final Logger log = LoggerFactory.getLogger(MyUtils.class);

    /**
     * 判断类中每个属性是否都为空
     *
     * @param o
     * @return
     */
    public static boolean allFieldIsNULL(Object o){
        try {
            for (Field field : o.getClass().getDeclaredFields()) {
                field.setAccessible(true);

                Object object = field.get(o);
                if (object instanceof CharSequence) {
                    if (!org.springframework.util.ObjectUtils.isEmpty(object)) {
                        return false;
                    }
                } else {
                    if (null != object) {
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            log.error("判断对象属性为空异常");
        }
        return true;
    }

    public static ErrorCode checkRegisterDtoOrResetPwdDto(Object obj) {
        String code;
        User user;
        log.info("instanceof 转换");
        if (obj instanceof RegisterDto) {
            code = ((RegisterDto) obj).getCode();
            user = ((RegisterDto) obj).getUser();
        } else {
            code = ((ResetDto) obj).getCode();
            user = ((ResetDto) obj).getUser();
        }

        // 验证码校验
        if (code == null || code.length() != 6) {
            return ErrorCode.VERITY_CODE_ILLEGAL;
        }
        // 对象校验
        if (user.getUserName() == null || user.getPassword() == null) {
            return ErrorCode.REGISTER_PARAS_NULL;
        }
        return null;
    }

}
